require 'rubygems'
require 'bit-struct'

require 'milfs/object/interface'

module Mil
  module FS
    # Ruby representation of a binary structure using the BitStruct
    # library.
    #
    # ---
    # *Structure*:: <tt> </tt> 
    # +created+:: <tt>Creation date for the file</tt>
    # +updated+:: <tt>Last date file was updated</tt>
    # +accessed+:: <tt>Last date the was accessed</tt>
    # +revision+:: <tt>Address of a FileNodeStruct that was the last revision of this file</tt>
    # +blocks_list+:: <tt>Address of the BlockListStruct for this file</tt>
    class FileNodeStruct < BitStruct
      float     :created,	    64,	    "Created date"
      float     :updated,	    64,	    "Last updated date"
      float     :accessed,    64,	    "Last accessed date"
      char      :revision,    320,    "Address to last revision (reserved)"
      char      :block_list,  320,    "Address to the file's block list"
    end

    # A wrapper class to encapsulate the data structure FileNodeStruct
    # and provide easy access to the structure.
    class FileNode < Mil::Object::Interface

      def initialize(block_list=nil)
	if block_list.nil?
	  @struct = FileNodeStruct.new
	  @struct.created = Time.now.to_f
	else
	  @struct = FileNodeStruct.new
	  @struct.created = Time.now.to_f unless @struct.created
	  @struct.block_list = block_list
	end
      end

      # This method will compare the internal wrapped structure with
      # that of the object passed in.
      def == (object)
	if ( truth = object.is_a? Mil::FS::FileNode )
	  truth |= @struct.size == object.struct.size
	  truth |= @struct.data == object.struct.data
	end

	truth
      end

      # This method returns a printable string representation 
      # of the wrapped Block structure.
      def dump
        "FileNode: \n" +
	"\tCreated: #{Time.at(@struct.created)}\n" +
	"\tUpdated: #{Time.at(@struct.updated)}\n" +
	"\tAccessed: #{Time.at(@struct.accessed)}\n"
      end

      # This method should be overriden to create an instance of the
      # wrapped structure type from the passed string and store it
      # in the @struct variable.
      def struct=(string)
	@struct = FileNodeStruct.new(string)
      end

      def created=(time)
	@struct.created = time
      end

      def created
	@struct.created
      end

      def updated=(time)
	@struct.updated = time
      end

      def updated
	@struct.updated
      end

      def accessed=(time)
	@struct.accessed = time
      end

      def accessed
	@struct.accessed
      end

      def revision=(revision)
	@struct.revision = revision
      end

      def revision
	@struct.revision
      end

      def block_list=(list)
	@struct.block_list = list
      end

      def block_list
	@struct.block_list
      end
    end
  end
end
